package fun.cyhgraph.controller.merchant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import fun.cyhgraph.annotation.AuthCheck;
import fun.cyhgraph.common.ErrorCode;
import fun.cyhgraph.common.UserConstant;
import fun.cyhgraph.exception.BusinessException;
import fun.cyhgraph.exception.ThrowUtils;
import fun.cyhgraph.model.dto.SetmealDTO;
import fun.cyhgraph.model.dto.SetmealPageDTO;
import fun.cyhgraph.model.entity.Setmeal;
import fun.cyhgraph.model.entity.User;
import fun.cyhgraph.model.merchantDto.SetmealMerchantDTO;
import fun.cyhgraph.model.merchantDto.SetmealPageMerchantDTO;
import fun.cyhgraph.result.PageResult;
import fun.cyhgraph.result.Result;
import fun.cyhgraph.service.MerchantInfoService;
import fun.cyhgraph.service.SetmealService;
import fun.cyhgraph.model.vo.SetmealVO;
import fun.cyhgraph.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/merchant/setmeal")
@Tag(name = "套餐管理")
@Slf4j
public class SetmealController {

    @Resource
    private SetmealService setmealService;
    @Resource
    private UserService userService;
    @Resource
    private MerchantInfoService merchantInfoService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @PostMapping
    @Operation(summary = "新增套餐")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO , HttpServletRequest request){
        SetmealMerchantDTO setmealMerchantDTO = new SetmealMerchantDTO();
        ThrowUtils.throwIf(setmealDTO == null , ErrorCode.PARAMS_ERROR);
        if (setmealDTO.getMerchantId() == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            setmealMerchantDTO.setMerchantId(loginUser.getId());
        }else {
            merchantInfoService.judgeMerchantInfo(setmealDTO.getMerchantId());
            setmealMerchantDTO.setMerchantId(setmealDTO.getMerchantId());
        }
        BeanUtils.copyProperties(setmealDTO, setmealMerchantDTO);
        setmealService.addSetmeal(setmealMerchantDTO);
        return Result.success();
    }

    /**
     * 套餐条件分页查询
     * @param setmealPageDTO
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "套餐条件分页查询")
    @PostMapping("/page")
    public Result<PageResult> getPageList(@RequestBody SetmealPageDTO setmealPageDTO , HttpServletRequest request){
        SetmealPageMerchantDTO setmealMerchantDTO = new SetmealPageMerchantDTO();
        ThrowUtils.throwIf(setmealPageDTO == null , ErrorCode.PARAMS_ERROR);
        if (setmealPageDTO.getMerchantId() == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            setmealMerchantDTO.setMerchantId(loginUser.getId());
            BeanUtils.copyProperties(setmealPageDTO, setmealMerchantDTO);
        }else {
            setmealMerchantDTO.setMerchantId(setmealPageDTO.getMerchantId());
            BeanUtils.copyProperties(setmealPageDTO, setmealMerchantDTO);
        }
        PageResult pageResult = setmealService.getPageList(setmealMerchantDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询套餐")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Integer id , @RequestParam(required = false) Integer merchantId, HttpServletRequest request){

        ThrowUtils.throwIf(id == null , ErrorCode.PARAMS_ERROR);
        if (merchantId == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            SetmealVO setmealVO = setmealService.getSetmealById(id , merchantId);
            return Result.success(setmealVO);
        }else {
            SetmealVO setmealVO = setmealService.getSetmealById(id , merchantId);
            return Result.success(setmealVO);
        }

    }

    /**
     * 根据id起售停售套餐
     * @param id
     * @return
     */
    @PutMapping("/status/{id}")
    @Operation(summary = "根据id起售停售套餐")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    public Result onOff(@PathVariable Integer id ,@RequestParam (required = false) Integer merchantId, HttpServletRequest request){
        if (merchantId == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            ThrowUtils.throwIf(id == null , ErrorCode.PARAMS_ERROR);
            setmealService.onOff(id , loginUser.getId());
        }else {
            //校验
            merchantInfoService.judgeMerchantInfo(merchantId);
            setmealService.onOff(id , merchantId);
        }

        return Result.success("成功");
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "修改套餐")
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO , HttpServletRequest request){
        ThrowUtils.throwIf(setmealDTO == null , ErrorCode.PARAMS_ERROR);

        SetmealMerchantDTO setmealMerchantDTO = new SetmealMerchantDTO();
        if (setmealDTO.getMerchantId() == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            setmealMerchantDTO.setMerchantId(loginUser.getId());
        }else {
            merchantInfoService.judgeMerchantInfo(setmealDTO.getMerchantId());
            setmealMerchantDTO.setMerchantId(setmealDTO.getMerchantId());
        }
        BeanUtils.copyProperties(setmealDTO, setmealMerchantDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealMerchantDTO, setmeal);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId, setmealDTO.getId())
                .eq(Setmeal::getMerchantId, setmeal.getMerchantId());
        //更新
        boolean update = setmealService.update(setmeal, queryWrapper);
        if (!update){
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR , "更新失败");
        }
        return Result.success("更新成功");
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除套餐")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Integer> ids ,Integer merchantId , HttpServletRequest request){
        if (merchantId == null){
            User loginUser = userService.getLoginUser(request);
            ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR);
            ThrowUtils.throwIf(ids == null , ErrorCode.PARAMS_ERROR);
            setmealService.deleteBatch(ids, loginUser.getId());
        }else {
            merchantInfoService.judgeMerchantInfo(merchantId);
            setmealService.deleteBatch(ids, merchantId);
        }
        return Result.success();
    }
}
